<%@page import="cn.ysnk.clz.interceptor.security.role.RoleEnum"%>
<%@page import="cn.ysnk.clz.entity.dataUser.DataUser"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
DataUser dUser = null;
String userId = (String)request.getAttribute("userId");
if(userId!=null){
	dUser = DataUser.userDao.findById(userId);
}
%>
</head>
<body>
	<div class="container">
		<script type="text/javascript">
			$(function(){
				var isCanSubmit = 1;
				jQuery("#editUserForm").validationEngine(
				'attach',
				{
					promptPosition : "topLeft",
					onSuccess : function() {
						if (isCanSubmit == 1) {
							jQuery("#editUserForm").submit();
							isCanSubmit++;
						}
				    }
			    });
			});
			function checkForm(){
				if($("#editUserForm").validationEngine("validate")){
					if($.trim($("#roleType").val())==""){
						$("#iconShow").validationEngine("showPrompt","请选择图标","error");
					}else{
						ajaxFormPost("/adminUser/saveUser","editUserForm",function(){refreshTab('/adminUser')},function(){refreshTab('/adminUser')});
					}
				}
			}
			
			function pageback(){
				refreshTab('/adminUser');
			}
		</script>
		<div class="page-header">
          <h2><%=dUser==null?"新增用户（初始密码为159753）":"编辑用户" %></h2>
        </div>
	   	<form class="form-horizontal" id="editUserForm" method="post">
	   	<input type="hidden" name="userId" value="<%=userId==null?"":userId %>"/>
		  <div class="form-group">
		    <label for="username" class="col-sm-2 control-label">用户名：</label>
		    <div class="col-sm-10">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input autocomplete="off" universal-class="validate[required,maxSize[7]]" type="text" class="form-control" <%=dUser==null?"":" readonly=\"readonly\"" %> value="<%=dUser==null?"":dUser.getStr("username") %>" id="username" name="username" placeholder="用户名">
		    </div>
		  </div>
		  <%
		  if(dUser!=null){
		  %>
		  <div class="form-group">
		    <label for="password" class="col-sm-2 control-label">修改密码：</label>
		    <div class="col-sm-10">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input autocomplete="off" universal-class="validate[required,maxSize[11],minSize[6]]" type="password" class="form-control" value="" id="password" name="password" placeholder="密码">
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="again_password" class="col-sm-2 control-label">确认密码：</label>
		    <div class="col-sm-10">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input autocomplete="off" universal-class="validate[required,maxSize[10],minSize[6],equals[password]]" type="password" class="form-control" value="" id="again_password" name="again_password" placeholder="确认密码">
		    </div>
		  </div>
		  <%
		  }
		  %>
		  <div class="form-group">
		    <label for="nickName" class="col-sm-2 control-label">昵称：</label>
		    <div class="col-sm-10">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input autocomplete="off" universal-class="validate[required,maxSize[7]]" type="text" class="form-control" value="<%=dUser==null?"":dUser.getStr("nickName") %>" id="nickName" name="nickName" placeholder="昵称">
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="nickName" class="col-sm-2 control-label">头像：</label>
		    <div class="col-sm-10">
	    		<% 
					String headIcon = "";
					String imageUrl = request.getContextPath()+"/images/user_default_photo.jpg";
					if (dUser != null && !(dUser.getStr("headIcon")==null)&&!"".equals(dUser.getStr("headIcon"))) {
						imageUrl = request.getContextPath() + "/upload/"+dUser.getStr("headIcon")+"?t="+new Date();
						headIcon = dUser.getStr("headIcon");
					}
					%>			
					<div style="width: 100px;height: 120px;border:1px solid;" id="peraonalImage">
						<img src='<%=imageUrl %>' style='width: 100px;height: 120px;'>
					</div>
					<div id="uploading"></div>
						<input type="hidden" id="headIcon" name="headIcon" value="<%=headIcon%>">
					<div>
						<input type="file" style="width: 140px;" name="userPic" id="userPic" onchange="checkAjaxFileUpload();" 
						universal-class="validate[custom[pictrue]]" style="width: 350px"/>
					</div>
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="icon" class="col-sm-2 control-label">角色：</label>
		    <div class="col-sm-10">
		    <input type="hidden" name="roleType" id="roleType" value="<%=dUser==null?"":dUser.getStr("roleType") %>"/>
		    <%
		    if(dUser==null){
				%>
		    	<div id="iconShow" name="iconShow" class="inline-block">无</div>
		    	<%
		    }else{
		    	%>
		    	<div id="iconShow" name="iconShow" class="inline-block">
		    		<%=RoleEnum.switch2RoleEnum(dUser.getStr("roleType")).getName() %>
		    	</div>
		    	<%
		    }
		    %>
		    <a href="<%=request.getContextPath() %>/adminUser/chooceRole" class="btn btn-default" data-toggle="modal" data-target="#myModal">选择图标</a>
		    </div>
		  </div>
		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <button type="button" onclick="checkForm();" class="btn btn-primary">保存</button>
		      <button type="button" onclick="pageback()" class="btn btn-default">返回</button>
		    </div>
		  </div>
		</form>
	</div>
	<script type="text/javascript">
	//上传事件
	function checkAjaxFileUpload(){
		var userpic = $("#userPic").val();
		var oldimage = $("#headIcon").val();
		alert(userpic);
		$("#uploading").html("<p>等待上传。。</p>");
			$.ajaxFileUpload({
				url:'<%=request.getContextPath()%>/adminUser/uploadIcon?oldimage='+oldimage,             //需要链接到服务器地址
				secureuri:false,
				fileElementId:'userPic',                        //文件选择框的id属性
				dataType: 'json',                                     //服务器返回的格式，可以是json
				success: function (data){            //相当于java中try语句块的用法
					$("#uploading").html("");
				console.log(data);
					if(data.error==0){
				  		$.jBox.prompt('上传成功','提示', 'success', {
				  			closed: function () { 
				  				$("#peraonalImage").html("<img src='<%=request.getContextPath()%>/upload/"+data.data+"' style='width: 100px;height: 120px;'>");
								$("#headIcon").val(data.data);
			  				} 
				  		});
				  	}else{
				  		$.jBox.prompt('上传失败','错误', 'error', {
				  			closed: function () { 
				  				var datatrim = $.trim(data.data);
								if("IMAGEERROR" == datatrim){
									alert("请上传图片，支持格式：gif、jpeg、jpg、png。");
								}else if("MAXSIZE" == datatrim){
									alert("图片最大为1M！"); 
								}else if("UPLOADERROR" == datatrim){
									alert("上传出错！");
								}
			  				} 
				  		});
				  	}
	             }
			});	 
	}
	</script>
</body>

</html>